<?php

namespace app\admin\model;

use think\Model;


class TeacherSubscribe extends Model
{

    

    

    // 表名
    protected $name = 'teacher_subscribe';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    


    //更新可约时间
    public static function up_subscribe($week,$teacher_id){
        //修改可约时间中间表
        $identity_info_model = new \app\admin\model\IdentityInfo();
        $week_name_arr = $identity_info_model->getWeekNameList();
        $add = [];
        $week_in = [];
        foreach ($week as $k=>$v){
            $add[$k]['teacher_id'] = $teacher_id;
            $add[$k]['week'] = isset($week_name_arr[$v]) ? $week_name_arr[$v] : 0;
            $add[$k]['class_id'] = 0;
            $week_in[] = isset($week_name_arr[$v]) ? $week_name_arr[$v] : 0;
        }
        $count = TeacherSubscribe::where(['teacher_id'=>$teacher_id])->where('class_id >0')->whereIn('week',$week_in)->count();
//        $count = TeacherSubscribe::where(['teacher_id'=>$teacher_id])->where('class_id >0')->whereIn('week',$week_in)->select();
//        $msg = '';
//        $msg_zy = '';
//        $arr_name_week = $identity_info_model->getWeekList();
//        foreach ($count as $k=>$v){
//            $week_name = $arr_name_week[$v['week']];
//            if($v['detail_id']>0){
//                $msg_zy = '';
//            }
//        }
        if($count>0){
            return ['code'=>0,'msg'=>'存在不可配置的可约时间'];
        }
        TeacherSubscribe::where(['teacher_id'=>$teacher_id,'class_id'=>0])->delete();
        TeacherSubscribe::insertAll($add);

        return ['code'=>1,'msg'=>'完成'];
    }

    //更新课程可约时间
    public static function up_class_subscribe($week,$teacher_id,$class_id,$old_week){
        //修改可约时间中间表
        $identity_info_model = new \app\admin\model\IdentityInfo();
        $week_name_arr = $identity_info_model->getWeekNameList();
        $add = [];
        $week_in = [];
        foreach ($week as $k=>$v){
            $add[$k]['teacher_id'] = $teacher_id;
            $add[$k]['week'] = $v;
            $add[$k]['class_id'] = $class_id;
            $week_in[] = $v;
        }
        $count = TeacherSubscribe::where(['teacher_id'=>$teacher_id])->where('class_id >0 and class_id !='.$class_id)->whereIn('week',$week_in)->count();
        if($count>0){
            return ['code'=>0,'msg'=>'存在不可配置的可约时间'];
        }
        TeacherSubscribe::where(['teacher_id'=>$teacher_id])->whereIn('week',$old_week)->update(['class_id'=>0]);
        TeacherSubscribe::where(['teacher_id'=>$teacher_id])->whereIn('week',$week_in)->delete();
        TeacherSubscribe::insertAll($add);

        IdentityInfo::up_week($teacher_id);
        return ['code'=>1,'msg'=>'完成'];
    }

    //课程完成更新课程可约时间
    public static function class_success_up($class_id,$teacher_id){
        TeacherSubscribe::where(['class_id'=>$class_id])->update(['class_id'=>0]);
        IdentityInfo::up_week($teacher_id);
        return ['code'=>1,'msg'=>'完成'];
    }





}
